<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>API 测试</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        button { padding: 10px 20px; margin: 10px; cursor: pointer; }
        pre { background: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto; }
        .success { color: green; }
        .error { color: red; }
    </style>
</head>
<body>
    <h1>ASR API 测试</h1>
    
    <button onclick="testGetModels()">1. 测试获取模型列表</button>
    <button onclick="testGetSpeaker()">2. 测试获取说话人模型</button>
    <button onclick="testCreateTask()">3. 测试创建任务</button>
    
    <h2>结果:</h2>
    <div id="result"></div>
    
    <script>
        const API_BASE = 'http://localhost:8080/api';
        
        function showResult(title, data, isError = false) {
            const resultDiv = document.getElementById('result');
            const className = isError ? 'error' : 'success';
            resultDiv.innerHTML = `
                <h3 class="${className}">${title}</h3>
                <pre>${JSON.stringify(data, null, 2)}</pre>
            `;
        }
        
        async function testGetModels() {
            try {
                const response = await fetch(`${API_BASE}/models/asr`);
                const result = await response.json();
                showResult('获取模型列表成功', result);
            } catch (error) {
                showResult('获取模型列表失败', error.message, true);
            }
        }
        
        async function testGetSpeaker() {
            try {
                const response = await fetch(`${API_BASE}/models/speaker`);
                const result = await response.json();
                showResult('获取说话人模型成功', result);
            } catch (error) {
                showResult('获取说话人模型失败', error.message, true);
            }
        }
        
        async function testCreateTask() {
            try {
                // 先获取模型配置
                const modelResponse = await fetch(`${API_BASE}/models/asr/paraformer-zh`);
                const modelResult = await modelResponse.json();
                const model = modelResult.data;
                
                const speakerResponse = await fetch(`${API_BASE}/models/speaker`);
                const speakerResult = await speakerResponse.json();
                const speaker = speakerResult.data;
                
                // 构建测试请求
                const request = {
                    audioFiles: ["/opt/speakr-ASR/audio/test.wav"],
                    config: {
                        speakerSegmentationModel: speaker.segmentation,
                        speakerEmbeddingModel: speaker.embedding,
                        asrModelPath: model.model,
                        asrTokensPath: model.tokens,
                        asrModelType: model.type,
                        numSpeakers: -1,
                        clusterThreshold: 0.7,
                        language: "zh",
                        numProcesses: 1,
                        numGpus: 1,
                        numThreads: 2,
                        sampleRate: 16000,
                        decodingMethod: "greedy_search",
                        useItn: true,
                        provider: "cuda"
                    }
                };
                
                console.log('发送请求:', request);
                
                const response = await fetch(`${API_BASE}/asr/tasks`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(request)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    showResult('创建任务成功', result);
                } else {
                    showResult(`创建任务失败 (${response.status})`, result, true);
                }
            } catch (error) {
                showResult('创建任务失败', error.message, true);
            }
        }
    </script>
</body>
</html>
